package logic

import (
	"context"
	"super-mart/models"

	"super-mart/martrpc/internal/svc"
	"super-mart/martrpc/martrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLogic {
	return &AddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 添加
func (l *AddLogic) Add(in *martrpc.AddRequest) (*martrpc.AddResponse, error) {
	// todo: add your logic here and delete this line

	shop := models.Shop{
		Name:  in.Name,
		Num:   int(in.Num),
		Price: float64(in.Price),
	}

	if err := shop.ShopAdd(models.DB); err != nil {
		return nil, err
	}

	return &martrpc.AddResponse{
		ShopId: int64(shop.ID),
	}, nil
}
